package com.xinsoft.controller.mes;

import cn.hutool.core.lang.Pair;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.MesDispatchHangUpParam;
import com.xinsoft.entity.dto.MesDispatchPageParam;
import com.xinsoft.entity.dto.MesDispatchReportParam;
import com.xinsoft.entity.dto.ScmPurchasePageParam;
import com.xinsoft.entity.po.MesProcessReport;
import com.xinsoft.entity.po.ScmPurchaseNotice;
import com.xinsoft.entity.vo.MesProcessCardVo;
import com.xinsoft.entity.vo.MesProcessReportAddVo;
import com.xinsoft.service.*;
import com.xinsoft.utils.TokenUtil;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 派工记录
 */
@RestController
@Validated
@RequestMapping("/scm/dispatch")
public class MesDispatchController {

    @Autowired
    private MesDispatchService mesDispatchService;
    @Autowired
    private QuaMaterialInspectionService quaMaterialInspectionService;


    /**
     * 删除
     */
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Long> ids) {
        //todo 结案状态的单据不能删除
        mesDispatchService.removeByIds(ids);
        return Response.succeed();
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody MesDispatchPageParam mesDispatchPageParam) {
        return mesDispatchService.findPageListByParam(mesDispatchPageParam);
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam @NotNull(message = "id不能为null") Integer id) {
        return mesDispatchService.detail(id);
    }

    /**
     * 开工
     */
    @PostMapping("/workStart")
    public Response<?> workStart(@RequestBody @Valid MesDispatchHangUpParam mesDispatchPageParam) {
        return mesDispatchService.workStart(mesDispatchPageParam);
    }

    /**
     * 挂起
     */
    @PostMapping("/workHangUp")
    public Response<?> workHangUp(@RequestBody @Valid MesDispatchHangUpParam mesDispatchPageParam) {
        return mesDispatchService.workHangUp(mesDispatchPageParam);
    }

    /**
     * 报工
     */
    @PostMapping("/report")
    public Response<?> report (@RequestBody @Valid MesProcessReportAddVo vo) {
        Pair<Integer, String> report = mesDispatchService.report(vo);
        if (report.getKey().equals(2)){
            return Response.fail(report.getValue());
        }
        //产品检验工序完工后推送检验单
        if (report.getKey().equals(1)){
            quaMaterialInspectionService.produceInspection(new Integer(report.getValue()));
        }
        return Response.succeed();
    }

    /**
     * 生产流转卡--获取上一道工序的报工数量
     */
    @PostMapping("/getReportNum")
    public Response<?> getReportNum (@RequestBody @Valid MesProcessCardVo vo) {
        return mesDispatchService.getReportNum(vo);
    }
}
